package com.platform.api;

import com.platform.annotation.LoginUser;
import com.platform.entity.ApiUserVideoBillVo;
import com.platform.entity.ApiUserVideoVo;
import com.platform.entity.UserInfo;
import com.platform.entity.UserVo;
import com.platform.service.ApiAmountBillService;
import com.platform.service.ApiUserService;
import com.platform.service.ApiUserVideoBillService;
import com.platform.service.ApiUserVideoService;
import com.platform.util.ApiBaseAction;
import com.platform.utils.Query;
import com.platform.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

/**
 * 作者: @author zhurusen <br>
 * 时间: 2017-08-11 08:32<br>
 * 描述: ApiIndexController <br>
 */
@RestController
@RequestMapping("/api/video")
public class ApiUserVideoController extends ApiBaseAction {

    @Autowired
    private ApiUserVideoBillService apiUserVideoBillService;
    @Autowired
    private ApiUserVideoService apiUserVideoService;
    @Autowired
    private ApiAmountBillService apiAmountBillService;
    @Autowired
    private ApiUserService apiUserService;


    /**
     * 发布小视频
     *
     * @param loginUser
     * @param payStatus
     * @param amount
     * @param content
     * @param videoUrl
     * @return
     */
    @RequestMapping("add")
    public R add(@LoginUser UserVo loginUser, Integer payStatus, BigDecimal amount, String content, String videoUrl) {
        ApiUserVideoVo apiUserVideoVo = new ApiUserVideoVo();
        apiUserVideoVo.setUserId(loginUser.getId());
        apiUserVideoVo.setCreateDate(new Date());
        apiUserVideoVo.setPayStatus(payStatus);
        apiUserVideoVo.setIsshow(1);
        apiUserVideoVo.setAmount(amount);
        apiUserVideoVo.setContent(content);
        apiUserVideoVo.setVideoUrl(videoUrl);
        apiUserVideoService.save(apiUserVideoVo);

        return R.ok(0, "请求成功", "");
    }

    /**
     * 查看
     *
     * @param loginUser
     * @param id
     * @return
     */
    @RequestMapping("view")
    public R view(@LoginUser UserVo loginUser, Long id) {
        if (loginUser != null) {
            Map<String, Object> param_bill = new HashMap();
            param_bill.put("userVideoId", id);
            param_bill.put("userId", loginUser.getId());

            List<ApiUserVideoBillVo> list = apiUserVideoBillService.selectList(param_bill);
            if (list.size() == 0) {
                ApiUserVideoBillVo apiUserVideoBillVo = new ApiUserVideoBillVo();
                apiUserVideoBillVo.setCreateDate(new Date());
                apiUserVideoBillVo.setUserId(loginUser.getId());
                apiUserVideoBillVo.setUserVideoId(id);
                apiUserVideoBillService.save(apiUserVideoBillVo);
            }
        }

        ApiUserVideoVo apiUserVideoVo = apiUserVideoService.selectOne(id);
        apiUserVideoVo.setView(apiUserVideoVo.getView() + 1);
        apiUserVideoService.updata(apiUserVideoVo);

        return R.ok(0, "请求成功", apiUserVideoVo.getView());
    }


    /**
     * @param loginUser
     * @param type      1 我喜欢的  2 我购买的
     * @param page
     * @return
     */
    @RequestMapping("list")
    public R list(@LoginUser UserVo loginUser, @RequestParam(value = "type", defaultValue = "1") Integer type, @RequestParam(value = "page", defaultValue = "1") Integer page) {
        Map<String, Object> params = new HashMap();
        if (loginUser.getMobile() != null && !loginUser.getMobile().equals("17798903668")) {//测试
            params.put("userId", loginUser.getId());
            if (type == 1) {
                params.put("like", 1);
            }
            if (type == 2) {
                params.put("payStatus", 1);
            }
        }


        params.put("page", page);
        params.put("limit", 10);

        params.put("sidx", "id");
        params.put("order", "desc");
        Query queryBill = new Query(params);

        List<ApiUserVideoVo> list = apiUserVideoService.selectList(queryBill);

        for (ApiUserVideoVo info : list) {
            UserInfo userInfo = apiUserService.toUserInfo(info.getUserId(), loginUser);

            /**
             * 封装对象
             */
            UserInfo vo = new UserInfo();
            vo.setId(userInfo.getId());
            vo.setAvatar(userInfo.getAvatar());
            vo.setNickName(userInfo.getNickName());
            vo.setLivePrice(userInfo.getLivePrice());
            info.setUserVo(vo);

            Map<String, Object> param_bill = new HashMap();
            param_bill.put("userVideoId", info.getId());
            param_bill.put("userId", loginUser.getId());

            List<ApiUserVideoBillVo> videoBillVos = apiUserVideoBillService.selectList(param_bill);
            if (videoBillVos.size() > 0) {
                ApiUserVideoBillVo apiUserVideoBillVo = videoBillVos.get(0);
                if (apiUserVideoBillVo.getLike() == 1) {
                    info.setIsLike(1);
                }
                if (apiUserVideoBillVo.getPayStatus() == 1) {
                    info.setPayStatus(2);
                }

            }

            if (loginUser.getMobile() != null && loginUser.getMobile().equals("17798903668")) {//测试
                info.setPayStatus(2);
            }
        }

        return R.ok(0, "请求成功", list);
    }

    @RequestMapping("like")
    public R like(@LoginUser UserVo loginUser, Long id) {
        ApiUserVideoVo apiUserVideoVo = apiUserVideoService.selectOne(id);

        Map<String, Object> param_bill = new HashMap();
        param_bill.put("userVideoId", id);
        param_bill.put("userId", loginUser.getId());


        List<ApiUserVideoBillVo> list = apiUserVideoBillService.selectList(param_bill);
        ApiUserVideoBillVo apiUserVideoBillVo = new ApiUserVideoBillVo();
        if (list.size() > 0) {
            apiUserVideoBillVo = list.get(0);
            apiUserVideoBillVo.setCreateDate(new Date());
            apiUserVideoBillVo.setLike(1);
            apiUserVideoBillVo.setUserId(loginUser.getId());
            apiUserVideoBillVo.setUserVideoId(id);
            apiUserVideoBillService.updata(apiUserVideoBillVo);
        } else {
            apiUserVideoBillVo.setCreateDate(new Date());
            apiUserVideoBillVo.setUserId(loginUser.getId());
            apiUserVideoBillVo.setUserVideoId(id);
            apiUserVideoBillVo.setLike(1);
            apiUserVideoBillService.save(apiUserVideoBillVo);
        }

        Map<String, Object> paramCount = new HashMap();
        paramCount.put("userVideoId", id);
        paramCount.put("like", 1);
        Integer count = apiUserVideoBillService.selectCountByVideoId(paramCount);

        apiUserVideoVo.setLike(count);
        apiUserVideoService.updata(apiUserVideoVo);

        return R.ok(0, "请求成功", count);
    }

    /**
     * 小视频解锁
     *
     * @param loginUser
     * @param id
     * @return
     */
    @RequestMapping("unlock")
    public R unlock(@LoginUser UserVo loginUser, Long id) {
        ApiUserVideoVo apiUserVideoVo = apiUserVideoService.selectOne(id);

        if (apiUserVideoVo != null) {
            if (apiUserVideoVo.getAmount().compareTo(loginUser.getAvailable_amount()) > 0) {
                return R.ok(1, "你的余额不足，请先充值", loginUser.getAvailable_amount().stripTrailingZeros().toPlainString());
            }


            Map<String, Object> param_bill = new HashMap();
            param_bill.put("userVideoId", id);
            param_bill.put("userId", loginUser.getId());
            List<ApiUserVideoBillVo> list = apiUserVideoBillService.selectList(param_bill);

            ApiUserVideoBillVo apiUserVideoBillVo = new ApiUserVideoBillVo();

            apiAmountBillService.consume(apiUserVideoVo.getUserId(), "videoPay", apiUserVideoVo.getAmount(), loginUser);

            if (list.size() > 0) {
                apiUserVideoBillVo = list.get(0);
                if (apiUserVideoBillVo.getPayStatus() != null && apiUserVideoBillVo.getPayStatus() == 2) {
                    apiUserVideoBillVo.setCreateDate(new Date());
                    apiUserVideoBillVo.setPayStatus(1);
                    apiUserVideoBillVo.setUserId(loginUser.getId());
                    apiUserVideoBillVo.setUserVideoId(id);
                    apiUserVideoBillService.updata(apiUserVideoBillVo);
                }
            } else {
                apiUserVideoBillVo.setCreateDate(new Date());
                apiUserVideoBillVo.setUserId(loginUser.getId());
                apiUserVideoBillVo.setUserVideoId(id);
                apiUserVideoBillVo.setPayStatus(1);
                apiUserVideoBillService.save(apiUserVideoBillVo);
            }

            UserVo userVo = apiUserService.queryObject(loginUser.getId());
            return R.ok(0, "请求成功", userVo.getAvailable_amount().stripTrailingZeros().toPlainString());

        }
        return R.ok(1, "请刷新数据", "");
    }

    /**
     * 指定主播发布的小视频
     *
     * @param loginUser
     * @param userId
     * @param page
     * @return
     */
    @RequestMapping("listByUserId")
    public R listByUserId(@LoginUser UserVo loginUser, Long userId, @RequestParam(value = "page", defaultValue = "1") Integer page) {
        Map<String, Object> params = new HashMap();
        params.put("zbUserId", userId);

        params.put("limit", 10);

        params.put("page", page);
        params.put("limit", 10);

        params.put("sidx", "id");
        params.put("order", "desc");
        Query queryBill = new Query(params);

        List<ApiUserVideoVo> list = apiUserVideoService.selectList(queryBill);
        for (ApiUserVideoVo info : list) {

            UserInfo userInfo = apiUserService.toUserInfo(info.getUserId(), loginUser);

            UserInfo vo = new UserInfo();
            vo.setId(userInfo.getId());
            vo.setAvatar(userInfo.getAvatar());
            vo.setNickName(userInfo.getNickName());
            vo.setLivePrice(userInfo.getLivePrice());

            info.setUserVo(vo);

            if (loginUser != null) {
                Map<String, Object> paramBill = new HashMap();
                paramBill.put("userVideoId", info.getId());
                paramBill.put("userId", loginUser.getId());
                List<ApiUserVideoBillVo> billVos = apiUserVideoBillService.selectList(paramBill);
                if (billVos.size() > 0) {
                    ApiUserVideoBillVo apiUserVideoBillVo = billVos.get(0);
                    if (apiUserVideoBillVo.getLike() == 1) {
                        info.setIsLike(1);
                    }
                    if (apiUserVideoBillVo.getPayStatus() == 1) {
                        info.setPayStatus(2);
                    }
                }
            }
        }

        return R.ok(0, "请求成功", list);
    }


    /**
     * 小视频 打赏
     *
     * @param loginUser
     * @return
     */
    @RequestMapping("reward")
    public R reward(@LoginUser UserVo loginUser, Long id, BigDecimal amount) {
        ApiUserVideoVo apiUserVideoVo = apiUserVideoService.selectOne(id);

        if (amount.compareTo(loginUser.getAvailable_amount()) > 0) {
            return R.ok(1, "你的余额不足，请先充值", loginUser.getAvailable_amount().stripTrailingZeros().toPlainString());
        }


        Map<String, Object> param_bill = new HashMap();
        param_bill.put("userVideoId", id);
        param_bill.put("userId", loginUser.getId());

        List<ApiUserVideoBillVo> list = apiUserVideoBillService.selectList(param_bill);

        ApiUserVideoBillVo apiUserVideoBillVo = new ApiUserVideoBillVo();
        if (list.size() > 0) {
            apiUserVideoBillVo = list.get(0);
            apiUserVideoBillVo.setCreateDate(new Date());
            apiUserVideoBillVo.setReward(1);
            apiUserVideoBillVo.setUserId(loginUser.getId());
            apiUserVideoBillVo.setUserVideoId(id);
            apiUserVideoBillVo.setAmount(apiUserVideoBillVo.getAmount().add(amount));
            apiUserVideoBillService.updata(apiUserVideoBillVo);
        } else {
            apiUserVideoBillVo.setCreateDate(new Date());
            apiUserVideoBillVo.setUserId(loginUser.getId());
            apiUserVideoBillVo.setUserVideoId(id);
            apiUserVideoBillVo.setAmount(amount);
            apiUserVideoBillVo.setReward(1);
            apiUserVideoBillService.save(apiUserVideoBillVo);
        }

        Map<String, Object> paramCount = new HashMap();
        paramCount.put("userVideoId", id);
        paramCount.put("reward", 1);
        Integer count = apiUserVideoBillService.selectCountByVideoId(paramCount);

        apiAmountBillService.consume(apiUserVideoVo.getUserId(), "videoReward", amount, loginUser);

        UserVo userVo = apiUserService.queryObject(loginUser.getId());
        Map<String, Object> reObject = new HashMap<>();
        reObject.put("count", count);
        reObject.put("availableAmount", userVo.getAvailable_amount().stripTrailingZeros().toPlainString());

        return R.ok(0, "请求成功", reObject);
    }

    /**
     * 取消点赞
     *
     * @param loginUser
     * @return
     */
    @RequestMapping("cancelike")
    public R cancellike(@LoginUser UserVo loginUser, Long id) {

        ApiUserVideoVo apiUserVideoVo = apiUserVideoService.selectOne(id);


        Map<String, Object> param_bill = new HashMap();
        param_bill.put("userVideoId", id);
        param_bill.put("userId", loginUser.getId());

        List<ApiUserVideoBillVo> list = apiUserVideoBillService.selectList(param_bill);
        for (ApiUserVideoBillVo info : list) {
            info.setLike(2);
            apiUserVideoBillService.updata(info);
        }
        Map<String, Object> paramCount = new HashMap();
        paramCount.put("userVideoId", id);
        paramCount.put("like", 1);
        Integer count = apiUserVideoBillService.selectCountByVideoId(paramCount);

        apiUserVideoVo.setLike(count);
        apiUserVideoService.updata(apiUserVideoVo);

        return R.ok(0, "请求成功", count);
    }

}